package day03.text03;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

public class Service implements Runnable {
    int port;

    public Service( int port) {
        this.port = port;
    }

    @Override
    public void run() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {

                Socket accept = serverSocket.accept();
                InputStream inputStream = accept.getInputStream();
           InputStreamReader ir = new InputStreamReader(inputStream, StandardCharsets.UTF_8);

              int read;
              while (true){
                   read = ir.read();

                  if (read==-1){
                      System.out.println("1");
                      accept.close();
                      serverSocket.close();
                      break;
                  }
                  System.out.print(read);
              }



            } catch (IOException e) {
                e.printStackTrace();
            }
        }

}
